#coding:utf-8
"""Copyright (c) 2022 God Forever
Alarm Clock is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details."""
import datetime
import pygame
import os
import time
import random
from pynput.keyboard import Key,Listener
all_songs=[]
all_songs_dict={}
for root, dirs, names in os.walk("d:/Users/{}/Music".format(os.getlogin())):
    for filename in names:
        if os.path.join(root, filename)[-4:] == ".mp3" or os.path.join(root, filename)[-4:] == ".ogg":
            all_songs.append(os.path.join(root, filename)[os.path.join(root, filename).find("\\") + 1:-4])
            all_songs_dict[
                os.path.join(root, filename)[os.path.join(root, filename).find("\\") + 1:-4]] = os.path.join(root,
                                                                                                             filename).replace(
                "\\", "/")
finding = open("alarm_list.txt", "a")
finding.close()
finding = open("alarm_list.txt", "r")
alarm_list_undone = finding.readlines()
finding.close()
alarm_list = []
for i in alarm_list_undone:
    alarm_list.append(i.replace("\n", ""))
finding = open("dates_one.txt", "a")
finding.close()
finding = open("dates_one.txt", "r")
dates_undone_one = finding.readlines()
finding.close()
finding = open("dates_two.txt", "a")
finding.close()
finding = open("dates_two.txt", "r")
dates_undone_two = finding.readlines()
finding.close()
dates = {}
for i in range(len(dates_undone_one)):
    dates[dates_undone_one[i].replace("\n", "")] =eval( dates_undone_two[i].replace("\n", ""))
finding = open("songs_one.txt", "a")
finding.close()
finding = open("songs_one.txt", "r")
songs_undone_one = finding.readlines()
finding.close()
finding = open("songs_two.txt", "a")
finding.close()
finding = open("songs_two.txt", "r")
songs_undone_two = finding.readlines()
finding.close()
songs = {}
for i in range(len(songs_undone_two)):
    try:
        open(songs_undone_two[i].replace("\n", ""),"r")
    except:
        songs_undone_two[i]=all_songs_dict[all_songs[random.randint(0,len(all_songs)-1)]]+"\n"
        with open("songs_two.txt", "w")as finding:
            finding.writelines(songs_undone_two)
for i in range(len(songs_undone_one)):
   songs[songs_undone_one[i].replace("\n", "")] = songs_undone_two[i].replace("\n", "")
finding = open("alarm_list.txt", "r")
finder=finding.read()
try:
    while finder[-1] == "\n":
        finder = finder[:-1]
except:
    while False:
        print()
with open("alarm_list.txt", "w") as finding:
    finding.write(finder)
finding = open("dates_one.txt", "r")
finder=finding.read()
try:
    while finder[-1] == "\n":
        finder = finder[:-1]
except:
    while False:
        print()
with open("dates_one.txt", "w") as finding:
    finding.write(finder)
finding = open("dates_two.txt", "r")
finder=finding.read()
try:
    while finder[-1] == "\n":
        finder = finder[:-1]
except:
    while False:
        print()
with open("dates_two.txt", "w") as finding:
    finding.write(finder)

finding = open("songs_one.txt", "r")
finder=finding.read()
try:
    while finder[-1] == "\n":
        finder = finder[:-1]
except:
    while False:
        print()
with open("songs_one.txt", "w") as finding:
    finding.write(finder)
finding = open("songs_two.txt", "r")
finder=finding.read()
try:
    while finder[-1] == "\n":
        finder = finder[:-1]
except:
    while False:
        print()
with open("songs_two.txt", "w") as finding:
    finding.write(finder)
def delete(i):
    global all_songs, all_songs_dict, alarm_list, dates, songs,alarm_list_undone,dates_undone_one,dates_undone_two,songs_undone_one,songs_undone_two
    try:
        try:
            alarm_list_undone.remove(i)
            alarm_list[-1] = alarm_list[-1].replace("\n", "")
        except:
            alarm_list_undone.remove(i + "\n")
    except:
        if False:
            print()
    alarm_list.remove(i)
    with open("alarm_list.txt", "w") as finding:
        finding.writelines(alarm_list_undone)
    for j in range(len(songs_undone_one)):
        if songs_undone_one[j] == i or songs_undone_one[j] == i + "\n":
            songs_undone_one.pop(j)
            songs_undone_two.pop(j)
            try:
                songs_undone_one[-1]=songs_undone_one[-1].replace("\n", "")
                songs_undone_two[-1]=songs_undone_two[-1].replace("\n", "")
            except:
                if False:
                    print()
    songs = {}
    for j in range(len(songs_undone_one)):
        songs[songs_undone_one[j].replace("\n", "")] = songs_undone_two[j].replace("\n", "")
    with open("songs_one.txt", "w") as finding:
        finding.writelines(songs_undone_one)
    with open("songs_two.txt", "w") as finding:
        finding.writelines(songs_undone_two)
    for j in range(len(dates_undone_one)):
        if dates_undone_one[j] == i or dates_undone_one[j] == i + "\n":
            dates_undone_one.pop(j)
            dates_undone_two.pop(j)
            try:
                dates_undone_one[-1]=dates_undone_one[-1].replace("\n", "")
                dates_undone_two[-1]=dates_undone_two[-1].replace("\n", "")
            except:
                if False:
                    print()
    dates = {}
    for j in range(len(dates_undone_one)):
        dates[dates_undone_one[j].replace("\n", "")] = dates_undone_two[j].replace("\n", "")
    with open("dates_one.txt", "w") as finding:
        finding.writelines(dates_undone_one)
    with open("dates_two.txt", "w") as finding:
        finding.writelines(dates_undone_two)
def play_it(url):
    global Listener
    pygame.mixer.init()
    t = pygame.mixer.music.load(url)
    pygame.mixer.music.play()

    def on_release(key):
        if key == Key.space:
            pygame.mixer.music.stop()

    with Listener(on_release=on_release) as Listener:
        Listener.join()
def alarm():
    global all_songs,all_songs_dict,alarm_list,dates,songs
    while True:
        try:
            e = time.time()
            finding = open("alarm_list.txt", "a")
            finding.close()
            finding = open("alarm_list.txt", "r")
            alarm_list_undone = finding.readlines()
            finding.close()
            alarm_list = []
            for i in alarm_list_undone:
                alarm_list.append(i.replace("\n", ""))
            finding = open("dates_one.txt", "a")
            finding.close()
            finding = open("dates_one.txt", "r")
            dates_undone_one = finding.readlines()
            finding.close()
            finding = open("dates_two.txt", "a")
            finding.close()
            finding = open("dates_two.txt", "r")
            dates_undone_two = finding.readlines()
            finding.close()
            dates = {}
            for i in range(len(dates_undone_one)):
                dates[dates_undone_one[i].replace("\n", "")] = eval(dates_undone_two[i].replace("\n", ""))
            finding = open("songs_one.txt", "a")
            finding.close()
            finding = open("songs_one.txt", "r")
            songs_undone_one = finding.readlines()
            finding.close()
            finding = open("songs_two.txt", "a")
            finding.close()
            finding = open("songs_two.txt", "r")
            songs_undone_two = finding.readlines()
            finding.close()
            songs = {}
            for i in range(len(songs_undone_two)):
                try:
                    open(songs_undone_two[i].replace("\n", ""), "r")
                except:
                    songs_undone_two[i] = all_songs_dict[all_songs[random.randint(0, len(all_songs) - 1)]] + "\n"
                    with open("songs_two.txt", "w") as finding:
                        finding.writelines(songs_undone_two)
            for i in range(len(songs_undone_one)):
                songs[songs_undone_one[i].replace("\n", "")] = songs_undone_two[i].replace("\n", "")
            finding = open("alarm_list.txt", "r")
            finder = finding.read()
            while finder[-1] == "\n":
                finder = finder[:-1]
            with open("alarm_list.txt", "w") as finding:
                finding.write(finder)
            finding = open("dates_one.txt", "r")
            finder = finding.read()
            while finder[-1] == "\n":
                finder = finder[:-1]
            with open("dates_one.txt", "w") as finding:
                finding.write(finder)
            finding = open("dates_two.txt", "r")
            finder = finding.read()
            while finder[-1] == "\n":
                finder = finder[:-1]
            with open("dates_two.txt", "w") as finding:
                finding.write(finder)

            finding = open("songs_one.txt", "r")
            finder = finding.read()
            while finder[-1] == "\n":
                finder = finder[:-1]
            with open("songs_one.txt", "w") as finding:
                finding.write(finder)
            finding = open("songs_two.txt", "r")
            finder = finding.read()
            while finder[-1] == "\n":
                finder = finder[:-1]
            with open("songs_two.txt", "w") as finding:
                finding.write(finder)
            for i in alarm_list:
                if str(datetime.datetime.now())[11:16] == i:
                    if time.strftime("%w", time.localtime()) in dates.get(i):
                        url = songs.get(i)
                        True_False=int(time.strftime("%Y", time.localtime())) < int(dates.get(i)[-1][4:8]) or (int(time.strftime("%Y", time.localtime())) == int(dates.get(i)[-1][4:8]) and int(time.strftime("%j", time.localtime())) <= int(dates.get(i)[-1][9:]))
                        if int(time.strftime("%Y", time.localtime())) > int(dates.get(i)[-1][4:8]) or (
                                int(time.strftime("%Y", time.localtime())) == int(dates.get(i)[-1][4:8]) and int(
                                time.strftime("%j", time.localtime())) >= int(dates.get(i)[-1][9:])):
                            delete(i)
                        if True_False:
                            play_it(url)
                else:
                    if int(time.strftime("%Y", time.localtime())) > int(dates.get(i)[-1][4:8]) or (
                            int(time.strftime("%Y", time.localtime())) == int(dates.get(i)[-1][4:8]) and int(
                        time.strftime("%j", time.localtime())) > int(dates.get(i)[-1][9:])):
                        delete(i)
                    if int(time.strftime("%Y", time.localtime())) == int(dates.get(i)[-1][4:8]) and int(time.strftime("%j", time.localtime())) == int(dates.get(i)[-1][9:]) and int(str(datetime.datetime.now())[11:13])*60+int(str(datetime.datetime.now())[14:16]) > int(i[:2])*60+int(i[3:]):
                        delete(i)
            time.sleep(1 - (time.time() - e))
        except:
            time.sleep(1 - (time.time() - e))
alarm()